lib.use(['element', 'table', 'form','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var tablePlug = layui.tablePlug //表格插件
    var configTable;

    //初始表格基础参数
    table.set(common.layui.table.config(tablePlug));


    // 渲染表格
    configTable = table.render({
            elem: '#configTable'
            , url: common.util.createUrl('/base/config/list')
            , cols: [[
                  {type:'checkbox'}
                , {type: 'numbers', title: '编号' }
                , {field: 'id', hide:true}
                , {field: 'paramKey', title: '参数名',  minwidth: 160}
                , {field: 'paramValue', title: '参数值', minwidth: 160}
                , {field: 'remark', title: '备注', width: 160}
                , {
                    fixed: 'right', title: '操作', width: 140, templet: function (row) {
                        return '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">修改</a>' +
                               '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    }
                }
            ]]
    });


    //监听行单击事件（单击事件为：rowDouble）
    table.on('rowDouble(configTableFilter)', function(obj){
        update(obj)
    });
    $(document).on('click','#del',function(){
        del(table.checkStatus('configTable'))
    });
    $(document).on('click','#update',function(){
        update(table.checkStatus('configTable'))
    });
    $(document).on('click','#add',function(){
        var config = {
            title:"新增参数管理",
            content: 'base/config/configForm.html?' //iframe的url，no代表不显示滚动条
        }
        common.util.openWindow(busCommon.baseWindowConfig(config))
    });


    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        params=common.from.serializeObject('#searchFrm');
        configTable.reload({where: params})

    }

    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1){common.util.error('请选择一条记录');return;}
        var config = {
            title:"新增参数管理",
            content: 'base/config/configForm.html?id=' + postData[0], //iframe的url，no代表不显示滚动条
        }
        common.util.openWindow(busCommon.baseWindowConfig(config))
    }

    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1){common.util.error('请选择一条记录');return;}
        layer.confirm('真的删除吗', function(index){
            console.log('Config Del param is', postData);
            common.ajax.post('/base/config/delete', postData, function (resp) {
                searchTable();
                layer.close(index);
            })

        });
    }
    window.formCallBack=function (params) {
        searchTable(params);
    };
});
